package com.getpebble.android.main.drawer.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.getpebble.android.common.model.PebbleColor;
import com.getpebble.android.notifications.util.NotificationUtil;
import com.getpebble.android.view.PblDeviceView;

public class DrawerHeaderView extends LinearLayout {
    private static final String TAG = DrawerHeaderView.class.getSimpleName();
    private LinearLayout mNotificationContainer;
    private ImageView mPebbleImage;
    private TextView mPebbleNameView;
    private TextView mPebbleStatusView;
    private TextView mPebbleTapToManage;

    public DrawerHeaderView(Context context) {
        this(context, null);
    }

    public DrawerHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawerHeaderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mPebbleNameView = null;
        this.mPebbleStatusView = null;
        this.mPebbleTapToManage = null;
        this.mPebbleImage = null;
        this.mNotificationContainer = null;
    }

    public void onFinishInflate() {
        super.onFinishInflate();
        this.mPebbleNameView = (TextView) findViewById(R.id.pebble_name);
        this.mPebbleStatusView = (TextView) findViewById(R.id.pebble_connection_status);
        this.mPebbleTapToManage = (TextView) findViewById(R.id.pebble_tap_to_manage);
        this.mPebbleImage = (ImageView) findViewById(R.id.pebble_connection_img);
        this.mNotificationContainer = (LinearLayout) findViewById(R.id.notification_bar_container);
        this.mNotificationContainer.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                NotificationUtil.showEnableServiceActivity(DrawerHeaderView.this.getContext());
            }
        });
    }

    public void setHeaderStatus(PblDeviceRecord device) {
        Drawable drawable;
        if (device == null) {
            this.mPebbleNameView.setText(R.string.connection_manager_disconnected);
            this.mPebbleNameView.setTextColor(getResources().getColor(R.color.default_orange_color));
            this.mPebbleStatusView.setText(R.string.drawer_tap_here_to_connect);
            this.mPebbleStatusView.setTextColor(getResources().getColor(R.color.white));
            this.mPebbleTapToManage.setVisibility(8);
            if (this.mPebbleImage.getDrawable() == null) {
                drawable = PblDeviceView.getConnectionDrawableForColor(getContext(), PebbleColor.COLOR_UNKNOWN);
                if (drawable != null) {
                    this.mPebbleImage.setImageDrawable(drawable);
                }
            }
            this.mPebbleImage.setVisibility(4);
            return;
        }
        this.mPebbleNameView.setText(device.pblDevice.getName());
        this.mPebbleNameView.setTextColor(getResources().getColor(R.color.white));
        this.mPebbleStatusView.setText(PblDeviceView.getStringForConnectionStatus(device));
        this.mPebbleStatusView.setTextColor(getResources().getColor(PblDeviceView.getTextColourResourceIdForConnectionStatus(device)));
        this.mPebbleTapToManage.setVisibility(0);
        this.mPebbleImage.setVisibility(0);
        drawable = PblDeviceView.getConnectionDrawableForColor(getContext(), device.color);
        if (drawable != null) {
            this.mPebbleImage.setImageDrawable(drawable);
        }
    }

    public void setShowNotificationBanner(boolean show) {
        if (show) {
            MobileAppBehavior.logAccessabilityDrawerBannerPosted();
            this.mNotificationContainer.setVisibility(0);
            return;
        }
        this.mNotificationContainer.setVisibility(8);
    }
}
